﻿using System;
using System.Collections.Generic;
using System.Linq;
using UcAsp.Net.Model;

namespace UcAsp.Net.NOSQLDAL
{
    public class AdminUserDAL : UcAsp.Net.IDAL.IAdminUserDAL
    {
        public IList<UcAsp.Net.Model.AdminUser> GetAdminUsers()
        {
            return STSData.GetInstance.Select<Model.AdminUser>();
        }

        public AdminUser Login(string username, string password)
        {
            if (GetAdminUsers().Count < 1)
            {
                Model.AdminUser user = new Model.AdminUser { ID = 1, UserName = "admin", UserPassword = "admin", LoginTime = DateTime.Now, LastLoginTime = DateTime.Now };
                new AdminUserDAL().Insert(user);
            }
            return GetAdminUsers().FirstOrDefault(o => o.UserName == username && o.UserPassword == password);
        }

        public bool Insert(AdminUser user)
        {
            long key;
            Model.AdminUser lastrow = STSData.GetInstance.LastRow<Model.AdminUser>(out key);
            user.ID = Convert.ToInt32(key.ToString()) + 1;
            STSData.GetInstance.Insert<Model.AdminUser>(user.ID, user);
            return true;
        }

        public bool Update(AdminUser user)
        {
            STSData.GetInstance.Update<Model.AdminUser>(user.ID, user);
            return true;
        }

        public bool Delete(AdminUser user)
        {
            STSData.GetInstance.Delete<Model.AdminUser>(user.ID);
            return true;
        }

        public List<AdminUser> Select()
        {
            return GetAdminUsers().ToList();
        }

        public AdminUser GetAdminByUserName(string username)
        {
            AdminUser userinfo = GetAdminUsers().FirstOrDefault(o => o.UserName == username);
            return userinfo;
        }
    }
}